package yxy.game.pm2.command.jadeseal;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.JadeSealMessage;
import yxy.game.pm2.module.jadeseal.JadeSeal;
import yxy.game.pm2.module.jadeseal.JadeSealModule;

/** 玉玺 玉玺界面 */
@MessageType(proto = Command.JadeSealInfo, classtype = JadeSealMessage.JadeSealInfoRequest.class)
public class CMD_JadeSealInfo extends Command<JadeSealMessage.JadeSealInfoRequest> {

	@Override
	public void execute(MessageSource messageSource, JadeSealMessage.JadeSealInfoRequest request) {
		String playerCode = messageSource.getTag();
		JadeSealMessage.JadeSealInfoResponse.Builder response = JadeSealMessage.JadeSealInfoResponse.newBuilder();

		JadeSealModule jsmodule = JadeSealModule.get(playerCode);
		if(jsmodule == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		for(JadeSeal js : jsmodule.getJss().values()) {
			JadeSealMessage.JSInfo.Builder build = JadeSealMessage.JSInfo.newBuilder();
			build.setCamp(js.getCamp());
			build.setUnlock(js.isOpen());
			if(js.isOpen()) {
				build.setExp(js.getExp());
				build.setLevel(js.getLevel());
				build.setStagelevel(js.getStagelevel());
				build.setStagestage(js.getStagestage());
			}
			response.addJsinfos(build.build());
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, JadeSealMessage.JadeSealInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}